<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>吉他商店管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .sidebar {
            width: 200px;
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            background-color: #333;
            padding-top: 20px;
        }
        .sidebar a {
            display: block;
            color: white;
            padding: 16px;
            text-decoration: none;
            transition: all 0.3s;
        }
        .sidebar a:hover {
            background-color: #555;
        }
        .sidebar a.active {
            background-color: #4CAF50;
        }
        .main-content {
            margin-left: 200px;
            padding: 20px;
        }
        .header {
            background-color: white;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .content {
            margin-top: 20px;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .dashboard {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            margin-bottom: 30px;
        }
        .stat-card {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            text-align: center;
        }
        .stat-card h3 {
            margin: 0;
            color: #666;
        }
        .stat-card .number {
            font-size: 24px;
            font-weight: bold;
            color: #4CAF50;
            margin: 10px 0;
        }
        .recent-records {
            margin-top: 30px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #4A90E2;
            color: white;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
            transition: background-color 0.3s;
        }
        .btn-logout {
            background-color: #f44336;
        }
        .welcome-text {
            font-size: 18px;
            color: #333;
        }
        .customer-list {
            margin-top: 30px;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .table-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 15% auto;
            padding: 20px;
            width: 50%;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .btn-group {
            display: flex;
            gap: 10px;
        }

        .btn-view {
            background-color: #2196F3;
        }

        .btn-edit {
            background-color: #FFA000;
        }
    </style>
</head>
<body>
<div class="sidebar">
    <a href="customer?action=list" class="active">顾客管理</a>
    <a href="guitar?action=list">吉他管理</a>
    <a href="purchase?action=list">购买记录</a>
</div>

<div class="main-content">
    <div class="header">
        <span class="welcome-text">欢迎, ${sessionScope.customer.username}</span>
        <button onclick="logout()" class="btn btn-logout">退出登录</button>
    </div>

    <div class="content">
        <div class="dashboard">
            <div class="stat-card">
                <h3>顾客总数</h3>
                <div class="number">${customerCount}</div>
            </div>
            <div class="stat-card">
                <h3>吉他库存</h3>
                <div class="number">${guitarCount}</div>
            </div>
            <div class="stat-card">
                <h3>交易总数</h3>
                <div class="number">${purchaseCount}</div>
            </div>
        </div>

        <div class="customer-list">
            <div class="table-header">
                <h2>顾客管理</h2>
                <button class="btn btn-add" onclick="showAddModal()">添加顾客</button>
            </div>
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>性别</th>
                    <th>电话</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${customers}" var="customer">
                    <tr>
                        <td>${customer.id}</td>
                        <td>${customer.username}</td>
                        <td>${customer.gender}</td>
                        <td>${customer.phone}</td>
                        <td class="btn-group">
                            <button class="btn btn-view" onclick="viewCustomer(${customer.id})">查看</button>
                            <button class="btn btn-edit" onclick="showEditModal(${customer.id})">编辑</button>
                            <button class="btn btn-delete" onclick="deleteCustomer(${customer.id})">删除</button>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div id="customerModal" class="modal">
    <div class="modal-content">
        <h2 id="modalTitle">添加顾客</h2>
        <form id="customerForm" action="customer" method="post">
            <input type="hidden" name="action" id="formAction" value="add">
            <div class="form-group">
                <label>用户名：</label>
                <input type="text" name="username" id="username" required>
            </div>
            <div class="form-group">
                <label>密码：</label>
                <input type="password" name="password" id="password" required>
            </div>
            <div class="form-group">
                <label>性别：</label>
                <select name="gender" id="gender">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="form-group">
                <label>电话：</label>
                <input type="tel" name="phone" id="phone" required>
            </div>
            <div class="btn-group">
                <button type="submit" class="btn btn-add">保存</button>
                <button type="button" class="btn btn-delete" onclick="hideModal()">取消</button>
            </div>
        </form>
    </div>
</div>

<div id="viewModal" class="modal">
    <div class="modal-content">
        <h2>顾客详情</h2>
        <div id="customerDetails">
            <p><strong>用户名：</strong> <span id="viewUsername"></span></p>
            <p><strong>性别：</strong> <span id="viewGender"></span></p>
            <p><strong>电话：</strong> <span id="viewPhone"></span></p>
        </div>
        <button class="btn btn-delete" onclick="hideViewModal()">关闭</button>
    </div>
</div>

<div id="editModal" class="modal">
    <div class="modal-content">
        <h2>编辑顾客</h2>
        <form id="editForm" action="customer" method="post">
            <input type="hidden" name="action" value="edit">
            <input type="hidden" name="id" id="editId">
            <div class="form-group">
                <label>用户名：</label>
                <input type="text" name="username" id="editUsername" required>
            </div>
            <div class="form-group">
                <label>新密码：</label>
                <input type="password" name="password" placeholder="留空表示不修改">
            </div>
            <div class="form-group">
                <label>性别：</label>
                <select name="gender" id="editGender">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="form-group">
                <label>电话：</label>
                <input type="tel" name="phone" id="editPhone" required>
            </div>
            <div class="btn-group">
                <button type="submit" class="btn btn-edit">保存</button>
                <button type="button" class="btn btn-delete" onclick="hideEditModal()">取消</button>
            </div>
        </form>
    </div>
</div>

<script>
    function showAddModal() {
        document.getElementById('modalTitle').textContent = '添加顾客';
        document.getElementById('formAction').value = 'add';
        document.getElementById('customerForm').reset();
        document.getElementById('customerModal').style.display = 'block';
    }

    function hideModal() {
        document.getElementById('customerModal').style.display = 'none';
    }

    function viewCustomer(id) {
        fetch('customer?action=get&id=' + id)
            .then(response => response.json())
            .then(customer => {
                document.getElementById('viewUsername').textContent = customer.username;
                document.getElementById('viewGender').textContent = customer.gender;
                document.getElementById('viewPhone').textContent = customer.phone;
                document.getElementById('viewModal').style.display = 'block';
            });
    }

    function hideViewModal() {
        document.getElementById('viewModal').style.display = 'none';
    }

    function deleteCustomer(id) {
        if (confirm('确定要删除这个顾客吗？')) {
            fetch('customer?action=delete&id=' + id)
                .then(response => response.json())
                .then(data => {
                    if (data.success === false) {
                        alert(data.message);
                    } else {
                        window.location.reload();
                    }
                })
                .catch(() => {
                    window.location.reload();
                });
        }
    }

    function logout() {
        if (confirm('确定要退出登录吗？')) {
            window.location.href = 'logout';
        }
    }

    function showEditModal(id) {
        fetch('customer?action=get&id=' + id)
            .then(response => response.json())
            .then(customer => {
                document.getElementById('editId').value = customer.id;
                document.getElementById('editUsername').value = customer.username;
                document.getElementById('editGender').value = customer.gender;
                document.getElementById('editPhone').value = customer.phone;
                document.getElementById('editModal').style.display = 'block';
            });
    }

    function hideEditModal() {
        document.getElementById('editModal').style.display = 'none';
    }

    document.getElementById('editForm').onsubmit = function(e) {
        e.preventDefault();
        const formData = new FormData(this);

        fetch('customer', {
            method: 'POST',
            body: formData
        })
            .then(response => response.text())
            .then(text => {
                try {
                    const data = JSON.parse(text);
                    if (data.success === false) {
                        alert(data.message);
                    } else {
                        window.location.reload();
                    }
                } catch {
                    window.location.reload();
                }
            });
    };
</script>
</body>
</html>